/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package operator.server;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import share.MessageType;
import share.Server;
import share.SocketCommunicator;

/**
 *
 * @author mire
 */
public class OperatorClientThread implements Runnable {

    private Socket clientSocket;
    private SocketCommunicator socComm;
    private OperatorServer operatorServer;
    private final SimpleDateFormat dateFormat;

    public OperatorClientThread(Socket clientSocket, OperatorServer operatorServer) {
        this.clientSocket = clientSocket;
        this.operatorServer = operatorServer;
        this.dateFormat = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy");
        this.socComm = null;
    }

    @Override
    public void run() {
        operatorServer.checkCentralServerOnline();

        String status = null;
        String threadInfo = dateFormat.format(new Date());
        threadInfo += " " + this.hashCode() + " ";
        try {
            socComm = new SocketCommunicator(clientSocket, operatorServer.getWaitForAnswerTime());
            socComm.open();
            String request = socComm.readString();
            String clientAddress = socComm.getAddress();

            threadInfo += "client " + clientAddress + " " + request + " ";
            System.out.println(threadInfo);

            MessageType msg;
            try {
                msg = MessageType.valueOf(request);
            } catch (IllegalArgumentException ex) {
                status = "ERROR_NO_REQUEST";
                System.out.println(threadInfo + status);
                socComm.writeString(status);
                socComm.close();
                return;
            }
            switch (msg) {
                case NEW_CLIENT: //<editor-fold defaultstate="collapsed" desc="...">
                {
                    String operatorName;

                    operatorName = socComm.readString();
                    if (!operatorName.equals(operatorServer.getOperatorName())) {
                        status = "ERROR_WRONG_OPERATOR";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    status = "REQUEST_ACCEPTED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);

                    operatorServer.getClients().add(clientAddress);
                    operatorServer.backupClients();
                    //synhronizuj sa ostalima
                    syncNewClientWithOthers(clientAddress);

                    status = "CLIENT_ADDED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);
                    socComm.close();
                    break;
                }
                //</editor-fold>

                case NEW_USER: //<editor-fold defaultstate="collapsed" desc="...">
                {

                    if (!operatorServer.getClients().contains(clientAddress)) {
                        status = "ERROR_UNKNOWN_CLIENT";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    status = "REQUEST_ACCEPTED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);

                    String userNumber = operatorServer.getFreeNumber();
                    OperatorUser newUser = new OperatorUser(userNumber, operatorServer.getOperatorName());

                    //dodaj kod sebe
                    Date synchronizedTime = new Date();
                    status = "ADDING_USER";
                    System.out.println(threadInfo + status);

                    newUser = new OperatorUser(userNumber, operatorServer.getOperatorName());
                    if (operatorServer.isCentralServerOnline()) {
                        operatorServer.getUsers().put(newUser.getId(), newUser);
                        operatorServer.backupUsers();
                    } else {
                        operatorServer.getCentralDownUsers().put(newUser.hashCode(), newUser);
                    }
                    operatorServer.setLastTimeSynchronized(synchronizedTime);
                    operatorServer.backupConfiguration();
                    //sinhonizuj sa serverima
                    status = "SYNCING_WITH_OTHERS";
                    System.out.println(threadInfo + status);
                    syncNewUserWithOthers(newUser, synchronizedTime);
                    //sinhronizuj sa centralnim serverom
                    status = "SYNCING_WITH_CENTRAL";
                    System.out.println(threadInfo + status);
                    syncNewUserWithCentral(newUser);
                    //zavrsheno
                    status = "USER_ADDED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);
                    socComm.writeString(newUser.getId() + "\t" + newUser.getNumber());
                    socComm.close();

                    break;
                }
                //</editor-fold>

                case NEW_MULTIPLE_USERS: //<editor-fold defaultstate="collapsed" desc="...">
                {

                    if (!operatorServer.getClients().contains(clientAddress)) {
                        status = "ERROR_UNKNOWN_CLIENT";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    status = "REQUEST_ACCEPTED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);

                    int numberOfUsers = socComm.readInt();

                    //dodaj kod sebe
                    status = "ADDING_MULTIPLE_USERS";
                    System.out.println(threadInfo + status);

                    ArrayList<String> newNumbers = new ArrayList<String>(numberOfUsers);
                    for (int i = 0; i < numberOfUsers; i++) {
                        newNumbers.add(operatorServer.getFreeNumber());
                    }
                    Date synchronizedTime = new Date();

                    ConcurrentHashMap<Integer, OperatorUser> newUsers = new ConcurrentHashMap<Integer, OperatorUser>(newNumbers.size());
                    for (Iterator<String> it = newNumbers.iterator(); it.hasNext();) {
                        OperatorUser user = new OperatorUser(it.next(), operatorServer.getOperatorName());
                        newUsers.put(user.getId(), user);
                    }

                    if (operatorServer.isCentralServerOnline()) {
                        operatorServer.getUsers().putAll(newUsers);
                        operatorServer.backupUsers();
                    } else {
                        operatorServer.getCentralDownUsers().putAll(newUsers);
                    }
                    operatorServer.setLastTimeSynchronized(synchronizedTime);
                    operatorServer.backupConfiguration();
                    //sinhonizuj sa serverima
                    status = "SYNCING_WITH_OTHERS";
                    System.out.println(threadInfo + status);
                    syncNewMultipleUsersWithOthers(newNumbers, newUsers, synchronizedTime);
                    //sinhronizuj sa centralnim serverom
                    status = "SYNCING_WITH_CENTRAL";
                    System.out.println(threadInfo + status);
                    syncNewMultipleUsersWithCentral(newUsers);
                    //zavrsheno
                    status = "MULTIPLE_USERS_ADDED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);
                    socComm.writeString(newUsers.toString());
                    socComm.close();
                    break;
                }
                //</editor-fold>

                case TRANSFER: //<editor-fold defaultstate="collapsed" desc="...">
                {
                    if (!operatorServer.isCentralServerOnline()) {
                        status = "ERROR_CENTRAL_SERVER_DOWN";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }
                    if (!operatorServer.getClients().contains(clientAddress)) {
                        status = "ERROR_UNKNOWN_CLIENT";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }
                    status = "REQUEST_ACCEPTED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);

                    String oldOperatorName = socComm.readString();
                    int userID = socComm.readInt();
                    String userNumber = socComm.readString();
                    //check number
                    if (operatorServer.getNumbers().contains(userNumber)
                            && !operatorServer.getUsers().containsKey(userID)) {
                        status = "ERROR_ID_NUMBER_MISMATCH";
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    if (operatorServer.getUsers().containsKey(userID)) {
                        OperatorUser user = operatorServer.getUsers().get(userID);
                        if (user.getCurrentOperator().equals(operatorServer.getOperatorName())) {
                            status = "ERROR_NUMBER_SAME_OPERATOR";
                            socComm.writeString(status);
                            socComm.close();
                            break;
                        }
                    }

                    //konektuj se na centralni server
                    status = "CONNECTING_TO_CENTRAL";
                    System.out.println(threadInfo + status);
                    status = connectCentralTransferUser(oldOperatorName, userID, userNumber);
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);
                    socComm.close();
                    break;

                }
                //</editor-fold>

                case MULTIPLE_TRANSFER: //<editor-fold defaultstate="collapsed" desc="...">
                {
                    if (!operatorServer.isCentralServerOnline()) {
                        status = "ERROR_CENTRAL_SERVER_DOWN";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    if (!operatorServer.getClients().contains(clientAddress)) {
                        status = "ERROR_UNKNOWN_CLIENT";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }
                    status = "REQUEST_ACCEPTED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);

                    String oldOperatorName;

                    oldOperatorName = socComm.readString();
                    String usersString = socComm.readString();

                    //check numbers
                    ArrayList<Integer> userIDs = operatorServer.parseUserIDs(usersString);
                    ArrayList<String> userNumbers = operatorServer.parseUserNumbers(usersString);
                    Iterator<Integer> itID = userIDs.iterator();
                    Iterator<String> itNum = userNumbers.iterator();
                    while (itID.hasNext() && itNum.hasNext()) {
                        int userID = itID.next();
                        String userNumber = itNum.next();

                        if (operatorServer.getNumbers().contains(userNumber)
                                && !operatorServer.getUsers().containsKey(userID)) {
                            status = "ERROR_ID_NUMBER_MISMATCH";
                            break;
                        }

                        if (operatorServer.getUsers().containsKey(userID)) {
                            OperatorUser user = operatorServer.getUsers().get(userID);
                            if (user.getCurrentOperator().equals(operatorServer.getOperatorName())) {
                                status = "ERROR_NUMBER_SAME_OPERATOR";
                                break;
                            }
                        }
                    }
                    if (!status.equals("REQUEST_ACCEPTED")) {
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    status = "CONNECTING_TO_CENTRAL";
                    System.out.println(threadInfo + status);
                    status = connectCentralTransferMultipleUsers(oldOperatorName, usersString);
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);
                    socComm.close();
                    break;
                }
                //</editor-fold>

                case GET_TRANSFER_LOG: //<editor-fold defaultstate="collapsed" desc="...">
                {
                    if (!operatorServer.getClients().contains(clientAddress)) {
                        status = "ERROR_UNKNOWN_CLIENT";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    String last30days = operatorServer.getTransferLog().getLast30DaysLog();
                    if (last30days == null) {
                        status = "ERROR_NO_LOG";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }
                    status = "REQUEST_ACCEPTED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);

                    socComm.writeString(last30days);
                    //zavrsheno
                    status = "LOG_SENT";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);
                    socComm.close();
                    break;
                }
                //</editor-fold>

                case DELETE_USER: //<editor-fold defaultstate="collapsed" desc="...">
                {
                    String userNumber;
                    int userID;

                    userID = socComm.readInt();
                    userNumber = socComm.readString();

                    if (!operatorServer.getClients().contains(clientAddress)) {
                        status = "ERROR_UNKNOWN_CLIENT";

                    }

                    if (!operatorServer.isCentralServerOnline()) {
                        status = "ERROR_CENTRAL_SERVER_DOWN";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }
                    if (!operatorServer.numberOk(userNumber)) {
                        status = "ERROR_NUMBER";
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    if (!operatorServer.getNumbers().contains(userNumber)) {
                        status = "ERROR_NO_NUMBER";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    if (operatorServer.getNumbers().contains(userNumber)
                            && !operatorServer.getUsers().containsKey(userID)) {
                        status = "ERROR_ID_NUMBER_MISMATCH";
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    if (!operatorServer.getUsers().get(userID).getCurrentOperator().equals(operatorServer.getOperatorName())) {
                        status = "ERROR_NO_NUMBER";
                        System.out.println(threadInfo + status);
                        socComm.writeString(status);
                        socComm.close();
                        break;
                    }

                    status = "REQUEST_ACCEPTED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);

                    //dodaj kod sebe
                    status = "DELETING_USER";
                    System.out.println(threadInfo + status);
                    Date synchronizedTime = new Date();
                    operatorServer.getNumbers().remove(userNumber);
                    operatorServer.backupUsers();
                    operatorServer.setLastTimeSynchronized(synchronizedTime);
                    operatorServer.backupConfiguration();

                    //sinhonizuj sa serverima
                    status = "SYNCING_WITH_OTHERS";
                    System.out.println(threadInfo + status);
                    syncDeleteUserWithOthers(userID, synchronizedTime);
                    //sinhronizuj sa centralnim serverom
                    status = "SYNCING_WITH_CENTRAL";
                    System.out.println(threadInfo + status);
                    syncDeleteUserWithCentral(userID, userNumber);
                    //zavrsheno
                    status = "USER_DELETED";
                    System.out.println(threadInfo + status);
                    socComm.writeString(status);
                    socComm.close();
                    break;
                }
                //</editor-fold>

            }
        } catch (SocketException ex) {
            Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(OperatorCentralThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (OutOfMemoryError err) {
            Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, err);
            try {
                socComm.writeString("ERROR_OOM");
            } catch (IOException ex) {
            }
        } finally {
            if (socComm != null) {
                try {
                    socComm.close();
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            status = "FINISHED";
            System.out.println(threadInfo + status);
        }
    }

    private void syncNewClientWithOthers(String clientAddress) {
        for (Iterator<Server> it = operatorServer.getServers().values().iterator(); it.hasNext();) {
            try {
                Server server = it.next();
                if (server.getAddress().equals(operatorServer.getAddress())) {
                    continue;
                }

                SocketCommunicator sco = new SocketCommunicator(new Socket(server.getAddress(), operatorServer.getOperatorPort()));
                try {
                    sco.open();
                    sco.writeString("NEW_CLIENT");
                    sco.readString(); //REQUEST_ACCEPTED

                    sco.writeString(clientAddress);

                    sco.readString(); //CLIENT_ADDED
                    sco.close();
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        sco.close();
                    } catch (IOException ex) {
                        Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } catch (UnknownHostException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void syncNewUserWithOthers(OperatorUser newUser, Date synchronizedTime) {

        for (Iterator<Server> it = operatorServer.getServers().values().iterator(); it.hasNext();) {
            try {
                Server server = it.next();
                if (server.getAddress().equals(operatorServer.getAddress())) {
                    continue;
                }

                SocketCommunicator sco = new SocketCommunicator(new Socket(server.getAddress(), operatorServer.getOperatorPort()));
                try {
                    sco.open();
                    sco.writeString("NEW_USER");
                    sco.readString(); //REQUEST_ACCEPTED

                    sco.writeObject(newUser);
                    sco.writeObject(synchronizedTime);

                    sco.readString();
                    sco.close();
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        sco.close();
                    } catch (IOException ex) {
                        Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } catch (UnknownHostException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void syncNewUserWithCentral(OperatorUser newUser) {
        if (operatorServer.isCentralServerOnline()) {
            SocketCommunicator scc = null;
            try {
                scc = new SocketCommunicator(new Socket(operatorServer.getCentralAddress(), operatorServer.getCentralPort()));
                scc.open();
                scc.writeString("NEW_USER");
                scc.readString(); //REQUEST_ACCEPTED

                scc.writeString(operatorServer.getOperatorName());

                scc.readString(); //OK

                scc.writeInt(newUser.getId());
                scc.writeString(newUser.getNumber());

                scc.readString();
                scc.close();
            } catch (IOException ex) {
                operatorServer.setCentralServerOnline(false);
            } finally {
                try {
                    if (scc != null) {
                        scc.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private void syncNewMultipleUsersWithOthers(ArrayList<String> newNumbers, ConcurrentHashMap<Integer, OperatorUser> newUsers, Date synchronizedTime) {
        for (Iterator<Server> it = operatorServer.getServers().values().iterator(); it.hasNext();) {
            try {
                Server server = it.next();
                if (server.getAddress().equals(operatorServer.getAddress())) {
                    continue;
                }

                SocketCommunicator sco = new SocketCommunicator(new Socket(server.getAddress(), operatorServer.getOperatorPort()));
                try {
                    sco.open();
                    sco.writeString("NEW_MULTIPLE_USERS");
                    sco.readString();

                    sco.writeObject(newNumbers);
                    sco.writeObject(newUsers);
                    sco.writeObject(synchronizedTime);

                    sco.readString();
                    sco.close();
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        sco.close();
                    } catch (IOException ex) {
                        Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } catch (UnknownHostException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void syncNewMultipleUsersWithCentral(ConcurrentHashMap<Integer, OperatorUser> newUsers) {
        if (operatorServer.isCentralServerOnline()) {
            SocketCommunicator scc = null;
            try {
                scc = new SocketCommunicator(new Socket(operatorServer.getCentralAddress(), operatorServer.getCentralPort()));
                scc.open();
                scc.writeString("NEW_MULTIPLE_USERS");
                scc.readString(); //REQUEST_ACCEPTED

                scc.writeString(operatorServer.getOperatorName());

                scc.readString(); //OK

                scc.writeString(newUsers.toString());

                scc.readString();
                scc.close();
            } catch (IOException ex) {
                operatorServer.setCentralServerOnline(false);
            } finally {
                try {
                    if (scc != null) {
                        scc.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }

    private String connectCentralTransferUser(String oldOperatorName, int userID, String userNumber) {
        SocketCommunicator scc = null;
        String status = null;
        int tries = 0;
        while (tries < 3) {
            try {
                scc = new SocketCommunicator(new Socket(operatorServer.getCentralAddress(), operatorServer.getCentralPort()), operatorServer.getAliveTimeout());

                scc.open();
                scc.writeString("TRANSFER");
                scc.writeString(operatorServer.getOperatorName());
                scc.writeString(oldOperatorName);
                scc.readString();

                scc.writeInt(userID);
                scc.writeString(userNumber);
                status = scc.readString();

                scc.close();
                break;
            } catch (SocketException ex) {
                ++tries;
            } catch (IOException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    scc.close();
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (tries == 3) {
            operatorServer.setCentralServerOnline(false);
            status = "ERROR_CENTRAL_SERVER_DOWN";
        }
        return status;
    }

    private String connectCentralTransferMultipleUsers(String oldOperatorName, String usersString) {
        SocketCommunicator scc = null;
        String status = null;
        int tries = 0;
        while (tries < 3) {
            try {
                scc = new SocketCommunicator(new Socket(operatorServer.getCentralAddress(), operatorServer.getCentralPort()), operatorServer.getAliveTimeout());

                scc.open();
                scc.writeString("MULTIPLE_TRANSFER");
                scc.writeString(operatorServer.getOperatorName());
                scc.writeString(oldOperatorName);
                scc.readString();

                scc.writeString(usersString);
                status = scc.readString();

                scc.close();
                break;
            } catch (SocketException ex) {
                ++tries;
            } catch (IOException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    scc.close();
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (tries == 3) {
            operatorServer.setCentralServerOnline(false);
            status = "ERROR_CENTRAL_SERVER_DOWN";
        }
        return status;
    }

    private void syncDeleteUserWithOthers(int userID, Date synchronizedTime) {
        for (Iterator<Server> it = operatorServer.getServers().values().iterator(); it.hasNext();) {
            Server server = it.next();
            if (server.getAddress().equals(operatorServer.getAddress())) {
                continue;
            }

            SocketCommunicator sco = null;
            try {

                sco = new SocketCommunicator(new Socket(server.getAddress(), operatorServer.getOperatorPort()));
                sco.open();
                sco.writeString("DELETE_USER");
                sco.writeInt(userID);
                sco.writeObject(synchronizedTime);

                sco.readString();
                sco.close();
            } catch (IOException ex) {
                Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    if (sco != null) {
                        sco.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private void syncDeleteUserWithCentral(int userID, String userNumber) {
        if (operatorServer.isCentralServerOnline()) {
            SocketCommunicator scc = null;
            try {
                scc = new SocketCommunicator(new Socket(operatorServer.getCentralAddress(), operatorServer.getCentralPort()));

                scc.open();
                scc.writeString("DELETE_USER");
                scc.readString(); //REQUEST_ACCEPTED

                scc.writeString(operatorServer.getOperatorName());
                scc.readString(); //OK

                scc.writeInt(userID);
                scc.writeString(userNumber);

                scc.readString(); //USER_DELETED
                scc.close();
            } catch (IOException ex) {
                operatorServer.setCentralServerOnline(false);
            } finally {
                try {
                    if (scc != null) {
                        scc.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(OperatorClientThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
